package org.pillar.fitness.web.controller.tienchin;

import org.pillar.fitness.common.annotation.Log;
import org.pillar.fitness.common.core.controller.BaseController;
import org.pillar.fitness.common.core.domain.AjaxResult;
import org.pillar.fitness.common.core.page.TableDataInfo;
import org.pillar.fitness.common.enums.BusinessType;
import org.pillar.fitness.common.utils.poi.ExcelUtil;
import org.pillar.fitness.course.domain.vo.CourseVo;
import org.pillar.fitness.course.service.ICourseService;
import org.pillar.fitness.course.validator.CreateGroup;
import org.pillar.fitness.course.validator.EditGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author javaboy
 * @since 2023-01-10
 */
@RestController
@RequestMapping("/fitness/course")
public class CourseController extends BaseController {

    @Autowired
    ICourseService courseService;


    /**
     * 获取课程列表
     */
    @PreAuthorize("hasPermission('fitness:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(CourseVo courseVo) {
        //前端传入的搜索参数
        //System.out.println("channelVO=" + channelVO);
        //父类的参数
        //System.out.println("channelVO.getParams()=" + channelVO.getParams());
        startPage();
        List<CourseVo> list = courseService.selectCourseList(courseVo);
        return getDataTable(list);
    }

    /**
     * 新增课程
     */
    @PreAuthorize("hasPermission('fitness:course:create')")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated(CreateGroup.class) @RequestBody CourseVo courseVo) {
        return courseService.addCourse(courseVo);
    }

    /**
     * 修改保存课程
     */
    @PreAuthorize("hasPermission('fitness:course:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated(EditGroup.class) @RequestBody CourseVo courseVo) {
        return courseService.updateCourse(courseVo);
    }

    /**
     * 根据课程 ID 查询一个具体课程
     *
     * @param courseId
     * @return
     */
    @PreAuthorize("hasPermission('fitness:course:edit')")
    @GetMapping("/{courseId}")
    public AjaxResult getInfo(@PathVariable Long courseId) {
        return AjaxResult.success(courseService.getById(courseId));
    }

    /**
     * 删除课程 可以批量删除
     */
    @PreAuthorize("hasPermission('fitness:course:remove')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{courseIds}")
    public AjaxResult remove(@PathVariable Long[] courseIds) {
        return toAjax(courseService.deleteCourseByIds(courseIds));
    }

    /**
     * 导出课程数据
     * @param response
     * @param courseVo
     */
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("hasPermission('fitness:course:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CourseVo courseVo) {
        List<CourseVo> list = courseService.selectCourseList(courseVo);
        ExcelUtil<CourseVo> util = new ExcelUtil<CourseVo>(CourseVo.class);
        util.exportExcel(response, list, "课程数据");
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<CourseVo> util = new ExcelUtil<CourseVo>(CourseVo.class);
        util.importTemplateExcel(response, "课程数据");
    }


    /**
     * 导入课程
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "课程管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("hasPermission('fitness:course:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<CourseVo> util = new ExcelUtil<CourseVo>(CourseVo.class);
        List<CourseVo> coursesList = util.importExcel(file.getInputStream());
        return AjaxResult.success(courseService.importCourse(coursesList,updateSupport));
    }
}
